var express = require('express');
var router = express.Router();
const adminsApi = require("../api/admins");


/* GET home page. */

router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});



/**
 * @swagger
 * /login:
 *    get:
 *      tags:
 *      - 管理员          # 接口分类
 *      summary: 管理员登录接口   # 接口备注
 *      description: 该接口用于后台管理系统管理员登录   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      parameters:
 *        - in: query
 *          name: adminName
 *          schema:
 *            type: string
 *            default: zhangsan
 *          description: 用户名
 *        - in: query
 *          name: adminPassword
 *          schema:
 *            type: string
 *            default: 111111
 *          description: 密码
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "登录成功，返回带有 token 属性的对象，空对象则是登录失败" # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                type: object
 *                properties:
 *                  _id:
 *                    type: string
 *                    description: 管理员编号
 *                  token:
 *                    type: string
 *                    description: 管理员token
 *              example:
 *                _id: "60541a3f25e17b406161a1d0"
 *                token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InpoYW5nc2FuIiwiaWF0IjoxNjIxMzI0MzIzLCJleHAiOjE2MjM5MTYzMjN9.wRSOhnQ8IXqW4K_TYWaQp6Sppa65PUbR9IMsgj8J3ck"
 * */
router.get('/login', async function (req, res, next) {
  const { adminName, adminPassword } = req.query
  const data = await adminsApi.login({ adminName, adminPassword })
  res.send(data)
});

/**,
 * @swagger
 * /reg:
 *    post:
 *      tags:
 *      - 管理员          # 接口分类
 *      summary: 管理员注册接口   # 接口备注
 *      description: 管理员注册   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/admins'
 *            example:
 *              username: "zhangsan"
 *              password: "123456"
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "注册成功"     # 返回 true
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/admins'
 * */
router.post('/reg', async function (req, res, next) {
  const { username, password } = req.body
  const data = await adminsApi.reg({ username, password })
  if (data._id) {
    res.send(true)
  }
});


/**
 * @swagger
 * /checkName:
 *    get:
 *      tags:
 *      - 管理员          # 接口分类
 *      summary: 管理员名字查重   # 接口备注
 *      description: 验证管理员输入的姓名是否重复   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      parameters:
 *        - in: query
 *          name: adminName
 *          schema:
 *            type: string
 *            default: zhangsan
 *          description: 用户名
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "查询成功" # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                type: object
 *                properties:
 *                  _id:
 *                    type: string
 *                    description: 管理员唯一编码
 *                  name:
 *                    type: string
 *                    description: 管理员姓名
 * 
 * */
router.get('/checkName', async function (req, res, next) {
  const { adminName } = req.query;
  const data = await adminsApi.checkName({ adminName })
  data ? res.send(true) : res.send(false)
});

/**
 * @swagger
 * /getList:
 *    get:
 *      tags:
 *      - 管理员          # 接口分类
 *      summary: 管理员列表   # 接口备注
 *      description: 返回管理员数据   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "查询成功" # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                type: object
 *                properties:
 *                  _id:
 *                    type: string
 *                    description: 管理员唯一编码
 *                  name:
 *                    type: string
 *                    description: 管理员姓名
 * 
 * */
router.get('/getList', async function (req, res, next) {
  const { curPage, eachPage } = req.query;
  const data = await adminsApi.getList({ curPage, eachPage });
  res.send(data)
});



/**,
 * @swagger
 * /deleteAdmin:
 *    delete:
 *      tags:
 *      - 管理员          # 接口分类
 *      summary: 管理员删除接口   # 接口备注
 *      description: 管理员验证密码   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/admins'
 *            example:
 *              username: "zhangsan"
 *              password: "123456"
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "成功"     # 返回 data
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/admins'
 * */
router.delete('/deleteAdmin', async function (req, res, next) {
  const { _id } = req.body;
  const data = await adminsApi.deleteAdmin({ _id });
  res.send(data)
})


/**,
 * @swagger
 * /checkPwd:
 *    post:
 *      tags:
 *      - 管理员          # 接口分类
 *      summary: 管理员验证密码接口   # 接口备注
 *      description: 管理员验证密码   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/admins'
 *            example:
 *              username: "zhangsan"
 *              password: "123456"
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "成功"     # 返回 true
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/admins'
 * */
router.post('/checkPwd', async function (req, res, next) {
  const { _id, adminPassword } = req.body;
  const data = await adminsApi.checkPwd({ _id, adminPassword });
  data.length ? res.send(true) : res.send(false)

})

/**,
 * @swagger
 * /updatePwd:
 *    patch:
 *      tags:
 *      - 管理员          # 接口分类
 *      summary: 管理员更新接口   # 接口备注
 *      description: 管理员更新数据   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/admins'
 *            example:
 *              adminName: "zhangsan"
 *              adminPassword: "123456"
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "成功"     # 返回 修改成功后的数据
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/admins'
 * */
router.patch('/updatePwd', async function (req, res, next) {
  const { _id, adminPassword, adminName } = req.body;
  const data = await adminsApi.updatePwd({ _id, adminPassword, adminName });
  res.send(data)
})






module.exports = router;
